# coding=UTF-8
import random
print("start to build train data pair!")
from const_ import *
#with open("data",encoding="utf-8") as f:
with open("./data.txt") as f:
    lines = f.readlines()

relation_dict = {}

def extract_x_num(line):
    count = 0
    if "?x" in line:
        count += 1
    if "?y" in line:
        count += 1
    if "( Min" in line \
        or "( Max" in line \
        or "( ArgMore" in line \
        or "( ArgMin" in line \
        or "( ArgMax" in line \
        or "( ArgLess" in line:
        count = 0
    return count


train_data = []

for i,line in enumerate(lines):
    if i%3==1:
        x_num = extract_x_num(line)
        if x_num!=0:
            one_pair = []
            one_pair.append(extract_id(line))
            one_pair.append(extract_question(lines[i-1]))
            one_pair.append(x_num)
            train_data.append(one_pair)


print("start to load word dict!")


word_dict = {}
with open("word_dict.txt","r") as f:
    lines2 = f.readlines()
    for line in lines2:
        temp = line.split(" ")
        word_dict[temp[0]] = int(temp[1].replace("\n",""))


for item in train_data:
    new_sentence = replace_entity_in_logic_form(item[1],lines[int(item[0])*3-2])
    final_sentence = replace_number_and_date_after_entity_removed(new_sentence)
    item[1] = final_sentence

with open("./train_nlpcc2.csv",mode="w") as f,open("./test_nlpcc2.csv",mode="w") as f2:
    f.write("id")
    f.write(",")
    f.write("text")
    f.write(",")
    f.write("x")
    f.write(",")
    f.write("xy")
    f.write(",")
    f.write("\n")

    f2.write("id")
    f2.write(",")
    f2.write("text")
    f2.write(",")
    f2.write("x")
    f2.write(",")
    f2.write("xy")
    f2.write(",")
    f2.write("\n")

    for index,item in enumerate(train_data):
        # if random.randint(1,10)!=1:
        if True:
            f.write("-1")
            f.write(",")
            f.write(item[1])
            f.write(",")
            temp = [0]*2
            temp[int(item[2])-1] = 1
            for i in temp:
                f.write(str(i))
                f.write(",")
            f.write("\n")
        else:
            f2.write("-1")
            f2.write(",")
            f2.write(item[1])
            f2.write(",")
            temp = [0] * 2
            temp[int(item[2])-1] = 1
            for i in temp:
                f2.write(str(i))
                f2.write(",")
            f2.write("\n")


